package cn.suimg.bilibili.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Cookie操作类
 */
public class Cookie {
    /**
     * Cookie
     */
    private Map<String,String> cookie = new HashMap<>();

    /**
     * 构造方法
     * @param cookie
     */
    public Cookie(String cookie){
        mixCookie(cookie);
    }

    public void mixCookie(String cookie){
        Arrays.asList(cookie.split(";")).forEach(c -> {
            String[] split = c.split("=");
            try{
                this.cookie.put(split[0].trim(),split[1].trim());
            }catch (ArrayIndexOutOfBoundsException e){
                try{
                    this.cookie.put(split[0].trim(),"");
                } catch (ArrayIndexOutOfBoundsException e2){}

            }

        });
    }
    public String toString(){
        StringBuilder sb = new StringBuilder();
        cookie.forEach((k,v) -> sb.append(String.format("%s=%s; ",k,v)));
        return sb.toString();
    }

    public String getValue(String name){
        return cookie.get(name);
    }


}
